<?php

namespace app\controller\web;

use app\service\AdminPathinfoService;
use app\validate\AdminPathinfoValidate;
use app\validate\PageValidate;
use share\service\ContextService;
use share\utils\Helper;

class AdminPathinfoController extends WebBaseController{

    public function initValidate(){        
        $this->validate = new AdminPathinfoValidate();
    }

    /**
     * 获取当前管理员权限信息
     */
    public function get_action(){
        $input = $this->check();
        $res = AdminPathinfoService::getAllRoleInfoByUser($input['id']);
        return self::ok(array_column($res,'rid'));        
    }

    /**
     * 设置当前管理员权限信息
     */
    public function set_action(){
        $input = $this->check();
        $res = AdminPathinfoService::setUseRoleInfo($input['id'],$input['roles']);
        return self::ok($res);   
    }    

    /**
     * 获取路由列表
     */
    public function get_all_path_action(){
        $input = $this->check();
        $pathType = isset($input['pathType']) ? $input['pathType'] : null;
        $res = AdminPathinfoService::getAllPathInfoByType($pathType);
        return self::ok($res);   
    }

    public function add_action(){
        $input = $this->check();
        $res = AdminPathinfoService::addPath($input);
        return self::ok($res);
    }

    public function edit_action(){
        $input = $this->check();
        $res = AdminPathinfoService::updatePath($input);
        return self::ok($res);
    }

    public function list_action(){        
        $page = $this->check('pageInfo',null,(new PageValidate()));   
        $data = AdminPathinfoService::getListInfo([],$page);        
        $data = Helper::arrayListReplaceKey($data,[
            'path_info' => 'pathInfo',
            'icon_type' => 'iconType',
            'path_type' => 'pathType',
            'check_login' => 'checkLogin',
            'check_auth' => 'checkAuth',
            'first_time' => 'firstTime',
            'modify_time' => 'modifyTime',
        ]);
        $total = AdminPathinfoService::getListCountInfo([]);
        return self::ok(self::formatPageData($page,$total,$data));    
    }

    public function del_action(){
        $input = $this->check();
        $res = AdminPathinfoService::delPath($input['id']);
        return self::ok($res);
    }
}